package cn.easier.activity.console.repository;

import cn.easier.activity.web.entity.PeriodicalMessage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 期刊 Repository
 */
@Repository
public interface ActivityPeriodicalRepository extends JpaRepository<PeriodicalMessage, Integer>, JpaSpecificationExecutor<PeriodicalMessage> {

    @Query(value = "select a.* from periodical_message a where ((:activityId is null or :activityId = '') or a.`id` = :activityId)  " +
            " and a.`status` <2  and a.create_by = :createBy  order by a.id desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<PeriodicalMessage> findByCreateByAndCreateTime(@Param("createBy") String createBy, @Param("activityId") Integer activityId, @Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.id) from periodical_message a where ((:activityId is null or :activityId = '') or a.`id` = :activityId) and " +
            " a.`status` <2  and a.create_by = :createBy  ", nativeQuery = true)
    long findAllByCount(@Param("createBy") String createBy, @Param("activityId") Integer activityId);

    @Query(value = "select a.* from periodical_message a where ((:activityId is null or :activityId = '') or a.`id` = :activityId)  " +
            " and a.`status` <2 order by a.id desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<PeriodicalMessage> findBy(@Param("activityId") Integer activityId, @Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.id) from periodical_message a where ((:activityId is null or :activityId = '') or a.`id` = :activityId) and " +
            " a.`status` <2 ", nativeQuery = true)
    long findByCount(@Param("activityId") Integer activityId);

    List<PeriodicalMessage> findByCreateByAndStatusIsNotOrderByCreateTimeDesc(String createBy, Integer status);

    List<PeriodicalMessage> findByStatusNotOrderByCreateTimeDesc(Integer status);

    PeriodicalMessage findByCreateByAndStatusIsNotAndId(String createBy, Integer status, Integer id);

    @Query(value = "UPDATE `periodical_message` SET STATUS = ? ,foreground_link = ?  WHERE id = ? AND create_by = ?", nativeQuery = true)
    @Modifying
    int release(Integer status,String foregroundLink, Integer id, String user);

    @Query(value = "UPDATE `periodical_message` SET release_flag = ? WHERE id = ? AND create_by = ?", nativeQuery = true)
    @Modifying
    int runOrPause(Boolean release, Integer id, String user);

    @Query(value = "UPDATE `periodical_message` SET release_flag = ? WHERE id = ? ", nativeQuery = true)
    @Modifying
    int runOrPauseOrSuperUser(Boolean release, Integer id);
}
